<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 400px;
            border: 1px solid #000;
            border-collapse: collapse;
            margin: 0 auto;
        }

        td,
        th {
            border: 1px solid #000;
            text-align: center;
        }

        input {
            width: 50px;
        }

        .search {
            width: 600px;
            margin: 20px auto;
        }
    </style>
</head>

<body>
    <div class="search">
        按照价格查询: <input type="text" class="start"> - <input type="text" class="end"> <button
            class="search-price">搜索</button> 按照商品名称查询: <input type="text" class="product"> <button
            class="search-pro">查询</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>id</th>
                <th>产品名称</th>
                <th>价格</th>
            </tr>
        </thead>
        <tbody>


        </tbody>
    </table>
    <script>
        const data = [{
            id: 1,
            pname: '小米',
            price: 3999
        },
        {
            id: 2,
            pname: 'oppo',
            price: 999
        },
        {
            id: 3,
            pname: '荣耀',
            price: 1299
        },

        {
            id: 4,
            pname: '华为',
            price: 1999
        },
        {
            id: 5,
            pname: '华为pro90',
            price: 7700
        },
        {
            id: 6,
            pname: '小米NOP10',
            price: 7700
        },

        ];
        const tbody = document.querySelector('tbody');
        const search = document.querySelector('.search-price')
        const start = document.querySelector('.start')
        const end = document.querySelector('.end');
        const pro = document.querySelector('.search-pro')
        const product = document.querySelector('.product');
        console.log(product);
        // console.log(pro);

        setData(data)


        // data.map(function (item) {
        //     const tr = document.createElement('tr');
        //     tr.innerHTML = `<td>${item.id}</td> <td>${item.pname}</td> <td>${item.price}</td>`;
        //     tbody.appendChild(tr);
        // });
        let newData;
        search.addEventListener('click', function () {
            // console.log(typeof start.value);
            Price();
            setData(newData);
        })

        function Price() {
            if (!start.value || !end.value) {
                alert('你倒是输入啊')
                return
            }

            // if (Number(start.value) !== NaN) {
            //     alert('请输入价格')
            //     return
            // }
            newData = data.filter(function (item) {
                return item.price >= start.value && item.price <= end.value;
            })
            // console.log(newData);
        };
        function setData(obj) {
            tbody.innerHTML = '';
            obj.map(function (item) {
                const tr = document.createElement('tr');
                tr.innerHTML = `<td>${item.id}</td> <td>${item.pname}</td> <td>${item.price}</td>`;
                tbody.appendChild(tr);
            });
        };

        pro.addEventListener('click', function () {
            // alert(11)
            sou()
        });
        // console.log(arr);

        function sou() {
            if (!product.value) {
                alert('请输入')
            }
            const arr = []

            // let flag = data.some(function (item) {

            data.filter(function (item) {
                if (item.pname.includes(product.value)) {
                    arr.push(item);

                }
            });
            // console.log(arr);
            if (arr == false) {
                alert(11)
                setData(data)
            } else {
                setData(arr);
            }

        }

        document.addEventListener('keyup', function (e) {
            // console.log(e.keyCode);
            if (e.keyCode == 13) {
                sou()
                product.value = '';
            }
            if (e.keyCode == 83) {
                product.focus();
            }
        })

    </script>
</body>

</html>